<?php

namespace app\service;

use app\exception\ErrnoPlus;
use app\model\UserLogModel;
use share\exception\ApiException;
use share\service\AmqpService;
use share\service\BaseModelService;
use share\service\ContextService;

class UserLogService extends BaseModelService
{
    // 设备
    const MODULE_OF_DEVICE = 'device';
    // 账户
    const MODULE_OF_ACCOUNT = 'account';
    // 设备转移
    const ACTION_OF_DEVICE_TRANSFER = "transfer";

    // 解绑
    const ACTION_OF_DEVICE_UNBIND = "unbind";

    // app用户
    const ACCOUNT_TYPE_OF_APP = 1;

    // 小工具用户
    const ACCOUNT_TYPE_OF_TOOLKIT = 2;

    // 后台管理员
    const ACCOUNT_TYPE_OF_WEBADMIN = 3;

    public static function getModel()
    {
        return new UserLogModel;
    }

    /**
     * 新增条目
     */
    public static function addUserLog($info)
    {
        self::add($info);
    }

    public static function addLogAsync(
        $account,
        $contentData,   
        $action = self::ACTION_OF_DEVICE_UNBIND,
        $module = self::MODULE_OF_DEVICE,
        $accountType = self::ACCOUNT_TYPE_OF_APP,
        $clientInfo = '',
        $ip = null
        ){
            $tn = time();
            if($ip  === null){
                $ip = request()->ip();
            }
            if(empty($clientInfo)){
                $clientInfo = Self::getClientInfoByAccountType($accountType);
            }
            $data = [
                'module' => $module,
                'action' => $action,
                'account' => $account,
                'account_type' => $accountType,
                'client_info' => $clientInfo,
                'ip' => $ip,
                'created' => $tn,
            ];
            if(is_string($contentData)){
                $content = $contentData; mb_substr($contentData,0,30000,'utf-8');
            }
            else{
                $content = json_encode($contentData,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
            }
            if(strlen($content) > 65530){
                $content = mb_substr($contentData,0,30000,'utf-8');
            }  
            $data['content'] = $content;

            // debug
            self::addUserLog($data);

            // AmqpService::simplePush([
            //     'service' => 'app\\service\\UserLogService@addUserLog',
            //     'params' => [
            //         $data
            //     ],
            // ]);
    }

    /**
     * @return string
     */
    public static function getClientInfoByAccountType($type){
        if ($type == self::ACCOUNT_TYPE_OF_WEBADMIN){
            return "web";
        }
        else{
            if($type == self::ACCOUNT_TYPE_OF_APP || $type == self::ACCOUNT_TYPE_OF_TOOLKIT){
                $data = ContextService::instance()->getAll();
                return sprintf("%s|%s|%s|%s|%s|%s"
                    ,isset($data[ContextService::KEY_PACK_NAME]) ? $data[ContextService::KEY_PACK_NAME] : ''
                    ,isset($data[ContextService::KEY_APP_VERSION]) ? $data[ContextService::KEY_APP_VERSION] : ''
                    ,isset($data[ContextService::KEY_CLIENT_TYPE]) ? $data[ContextService::KEY_CLIENT_TYPE] : ''
                    ,isset($data[ContextService::KEY_CLIENT_NAME]) ? $data[ContextService::KEY_CLIENT_NAME] : ''
                    ,isset($data[ContextService::KEY_DEVICE_ID]) ? $data[ContextService::KEY_DEVICE_ID] : ''
                    ,isset($data[ContextService::KEY_VENDOR]) ? $data[ContextService::KEY_VENDOR] : ''
                );
            }
        }
        return "";
    }

    /**
     * 页面查询条件
     */
    public static function buildCondition($filter)
    {
        $res = [];
        if (isset($filter['account']) && !empty($filter['account'])) {
            $res['account'] = ['like', '%' . $filter['account'] . '%'];
        }
        if (isset($filter['keyword']) && !empty($filter['keyword'])) {
            $res['content'] = ['like', '%' . $filter['keyword'] . '%'];
        }
        if (isset($filter['module']) && !empty($filter['module'])) {
            $res['module'] = $filter['module'];
        }
        if (isset($filter['action']) && !empty($filter['action'])) {
            $res['action'] = $filter['action'];
        }        
        return $res;
    }
}
